import Vue from 'vue'
import Router from 'vue-router'
import {
  Toast
} from 'vant';
Vue.use(Router)
Vue.use(Toast);
const originalReplace = Router.prototype.replace;
Router.prototype.replace = function replace(location) {
  return originalReplace.call(this, location).catch(err => err)
}
const VueRouterPush = Router.prototype.push;
Router.prototype.push = function push(to) {
  return VueRouterPush.call(this, to).catch(err => err)
}
var router = new Router({
  // mode: "history",
  // scrollBehavior: () => ({y:0}),
  routes: [
    {
      path: '/wechatAuthorization',
      name: 'wechatAuthorization',
      component: () => import('@/components/wechatAuthorization'),
    }, {
      path: '/login',
      name: 'login',
      component: () => import('@/components/login')
    },
    // 首页
    {
      path: '/',
      name: 'index',
      component: () => import('@/common/index'),
      redirect: '/home',
      children: [{
          path: 'home',
          name: 'home',
          component: () => import('@/views/dashboard/index'),
          meta: {
            requireAuth: true,
            navShow: true
          },
        },
        // 患者列表
        {
          path: 'patient/:ysid',
          name: 'patient',
          component: () => import('@/views/patient/index'),
          meta: {
            requireAuth: true,
            navShow: true
          },
        },
        // 医院列表
        {
          path: 'hospitalList',
          name: 'hospitalList',
          component: () => import('@/views/patient/hospitalList'),
          meta: {
            requireAuth: true,
            navShow: true
          },
        },
        // 医生列表
        {
          path: 'myDoctor/:yyid',
          name: 'myDoctor',
          component: () => import('@/views/patient/MyDoctorList'),
          meta: {
            requireAuth: true,
            navShow: true
          },
        },
  
        // 顾问查看医生
        {
          path: 'Doctor/:yyid',
          name: 'Doctor',
          component: () => import('@/views/patient/doctor'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 顾问查看医院
        {
          path: 'hospital',
          name: 'hospital',
          component: () => import('@/views/patient/hospital'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 新增患者
        {
          path: 'addPatient',
          name: 'addPatient',
          component: () => import('@/views/patient/add'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 编辑患者
        {
          path: 'editPatient/:id',
          name: 'editPatient',
          component: () => import('@/views/patient/editPatient'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 患者二维码
        {
          path: 'qrCode',
          name: 'qrCode',
          component: () => import('@/views/patient/qrCode'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 患者详情
        {
          path: 'detail/:id',
          name: 'detail',
          component: () => import('@/views/patient/detail'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 新增健康档案
        {
          path: 'record/:detail',
          name: 'record',
          component: () => import('@/views/patient/records'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 新增营养处方
        {
          path: 'newNutritionPrescription/:id',
          name: 'newNutritionPrescription',
          component: () => import('@/views/patient/newNutritionPrescription'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 预览营养处方
        {
          path: 'preview',
          name: 'preview',
          component: () => import('@/views/patient/preview'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 预览处方中方案
        {
          path: 'fangan',
          name: 'fangan',
          component: () => import('@/views/patient/fangan'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 编辑营养处方
        {
          path: 'editNutritionPrescription/:id/:hzid',
          name: 'editNutritionPrescription',
          component: () => import('@/views/patient/editNutritionPrescription'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        }, 
        // 营养处方列表
        {
          path: 'NutritionPrescriptionList/:id',
          name: 'NutritionPrescriptionList',
          component: () => import('@/views/patient/NutritionPrescriptionList'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },      
        // 营养处方详情
        {
          path: 'NutritionPrescriptionDetails/:id/:hzid',
          name: 'NutritionPrescriptionDetails',
          component: () => import('@/views/patient/NutritionPrescriptionDetails'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 新增营养方案
        {
          path: 'TreatmentPlan/:id',
          name: 'TreatmentPlan',
          component: () => import('@/views/patient/TreatmentPlan'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 历史营养方案列表
        {
          path: 'HistoricalPlan/:id',
          name: 'HistoricalPlan',
          component: () => import('@/views/patient/HistoricalPlan'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 方案详情
        {
          path: 'HistoricalCases/:id',
          name: 'HistoricalCases',
          component: () => import('@/views/patient/HistoricalCases'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 健康档案详情
        {
          path: 'caseDetails/:id',
          name: 'caseDetailss',
          component: () => import('@/views/patient/caseDetails'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 健康日志列表
        {
          path: 'HealthLogList/:pid',
          name: 'HealthLogList',
          component: () => import('@/views/patient/HealthLogList'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 健康日志详情
        {
          path: 'HealthLogDetails/:id',
          name: 'HealthLogDetails',
          component: () => import('@/views/patient/HealthLogDetails'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 0版本健康日志
        {
          path: 'logDetails/:id',
          name: 'logDetails',
          component: () => import('@/views/patient/logDetails'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 营养方案详情
        {
          path: 'TreatmentPlanDetails/:id',
          name: 'TreatmentPlanDetails',
          component: () => import('@/views/patient/TreatmentPlanDetails'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 膳食处方
        {
          path: 'SoftFood/:item/:pid',
          name: 'SoftFood',
          component: () => import('@/views/patient/SoftFood'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        {
          path: 'SemiLiquidFood/:item',
          name: 'SemiLiquidFood',
          component: () => import('@/views/patient/SemiLiquidFood'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 服用产品
        {
          path: 'HomogEnate/:item',
          name: 'HomogEnate',
          component: () => import('@/views/patient/HomogEnate'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 文本模板
        {
          path: 'textTemplate',
          name: 'textTemplate',
          component: () => import('@/views/patient/textTemplate'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 视频列表
        {
          path: 'videoList',
          name: 'videoList',
          component: () => import('@/views/patient/videoList'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 服用产品
        {
          path: 'TakingProducts/:index',
          name: 'TakingProducts',
          component: () => import('@/views/patient/TakingProducts'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 服用产品
        {
          path: 'ExtraMeal/:index',
          name: 'ExtraMeal',
          component: () => import('@/views/patient/ExtraMeal'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 服用产品
        {
          path: 'medicine/:index',
          name: 'medicine',
          component: () => import('@/views/patient/medicine'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 营养顾问模块 弃用
        // {
        //   path: 'consultant',
        //   name: 'consultant',
        //   component: () => import('@/views/Consultant/index'),
        //   meta: {
        //     requireAuth: true,
        //     navShow: true
        //   },
        // },
        // {
        //   path: 'remind',
        //   name: 'remind',
        //   component: () => import('@/views/Consultant/remind'),
        //   meta: {
        //     requireAuth: true,
        //     navShow: false
        //   },
        // },
        // 我的
        {
          path: 'my',
          name: 'my',
          component: () => import('@/views/my/index'),
          meta: {
            requireAuth: true,
            navShow: true
          },
        },
        // 个人信息
        {
          path: 'AccountInformation',
          name: 'AccountInformation',
          component: () => import('@/views/my/accountinformation'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 顾问管理
        {
          path: 'myAdviserList',
          name: 'myAdviserList',
          component: () => import('@/views/my/adviserlist'),
          meta: {
            requireAuth: true,
            navShow: true
          },
        },
        // 顾问管理医院列表
        {
          path: 'myHospitalList',
          name: 'myHospitalList',
          component: () => import('@/views/my/hospitalList'),
          meta: {
            requireAuth: true,
            navShow: true
          },
        },
        // 顾问管理医生列表
        {
          path: 'myDoctorList/:yyid',
          name: 'myDoctorList',
          component: () => import('@/views/my/MyDoctorList'),
          meta: {
            requireAuth: true,
            navShow: true
          },
        },
        // 顾问管理查看患者
        {
          path: 'adviserPatient/:id',
          name: 'adviserPatient',
          component: () => import('@/views/my/adviserPatient'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 密码修改
        {
          path: 'changePassword',
          name: 'changePassword',
          component: () => import('@/views/my/changePassword'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 意见反馈
        {
          path: 'feedback',
          name: 'feedback',
          component: () => import('@/views/my/feedback'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 直播平台
        {
          path: 'zhibo',
          name: 'zhibo',
          component: () => import('@/views/patient/zhibo'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 聊天列表
        {
          path: 'board',
          name: 'board',
          component: () => import('@/views/patient/board'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 协议
        {
          path: 'xieyi',
          name: 'xieyi',
          component: () => import('@/views/my/xieyi'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 聊天yemian
        {
          path: 'single',
          name: 'single',
          component: () => import('@/views/patient/single'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 患者详细信息
        {
          path: 'patientDetail/:id',
          name: 'patientDetail',
          component: () => import('@/views/patient/patientDetail'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 健康日志首页
        {
          path: 'journalIndex/:id',
          name: 'journalIndex',
          component: () => import('@/views/journal/index'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 身体及运动
        {
          path: 'BodyAndExercise/:hzid',
          name: 'BodyAndExercise',
          component: () => import('@/views/journal/BodyAndExercise'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 营养饮食
        {
          path: 'food/:hzid',
          name: 'food',
          component: () => import('@/views/journal/food'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 健康日志食物列表
        {
          path: 'journalTreatmentPlan/:item',
          name: 'journalTreatmentPlan',
          component: () => import('@/views/journal/TreatmentPlan'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 身体症状
        {
          path: 'Physical/:hzid',
          name: 'Physical',
          component: () => import('@/views/journal/Physical'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 血液指标
        {
          path: 'bool/:hzid',
          name: 'bool',
          component: () => import('@/views/journal/bool'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 用药情况
        {
          path: 'DrugUse/:hzid',
          name: 'DrugUse',
          component: () => import('@/views/journal/DrugUse'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 日常曲线
        {
          path: 'line/:hzid',
          name: 'line',
          component: () => import('@/views/journal/line'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // 饮食评分曲线
        {
          path: 'dietCurve/:hzid',
          name: 'dietCurve',
          component: () => import('@/views/patient/dietCurve'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // PG-SGA
        {
          path: 'newPG-SGA/:id',
          name: 'newPG-SGA',
          component: () => import('@/views/patient/newPG-SGA'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        {
          path: 'pg-SGAList/:id',
          name: 'pg-SGAList',
          component: () => import('@/views/patient/pg-SGAList'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        {
          path: 'pg-SGA',
          name: 'pg-SGA',
          component: () => import('@/views/patient/pg-SGA'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        // nrs2002
        {
          path: 'newNrs/:id',
          name: 'newNrs',
          component: () => import('@/views/patient/newNrs'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        {
          path: 'nrsList/:id',
          name: 'nrsList',
          component: () => import('@/views/patient/nrsList'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
        {
          path: 'nrs2002',
          name: 'nrs2002',
          component: () => import('@/views/patient/nrs2002'),
          meta: {
            requireAuth: true,
            navShow: false
          },
        },
      ]
    },
    // 错误页面重定向到首页
    {
      path: '*',
      redirect: "/"
    },
  ]
})

router.beforeEach((to, from, next) => {

  // 新增营养处方需要用到localStorage 同时需要跨页面选择medicine
  if ( to.name !== "addPatient" && to.name !== "SoftFood" && to.name !== "textTemplate" && to.name !== "videoList" && to.name !== "ExtraMeal" && to.name !== "medicine" && to.name !== "editNutritionPrescription" && to.name !== "newNutritionPrescription"  && to.name!=="NutritionPrescriptionList" && to.name !== "TakingProducts" && to.name !== "qrCode" && to.name !== "food" && to.name !== "journalTreatmentPlan" && to.name !== "journalIndex" && to.name !== "editPatient" ) {
    localStorage.removeItem("form")
    localStorage.removeItem("oral")
    localStorage.removeItem("extrameals")
    localStorage.removeItem("exercise")
    localStorage.removeItem("medicine")
    localStorage.removeItem("hzid")
    localStorage.removeItem("ysid")
    sessionStorage.removeItem("open")
  }
  if(process.env.NODE_ENV==='development'){
    sessionStorage.setItem('token','9df7243689b902de1ba1ededed61c8d1')
  }
  // 在判断时，需要将 login 路由排除掉
  if (to.meta.requireAuth == true) {
    var local = window.location.href; // 获取页面url
    local = local.split('#')[1]
    localStorage.setItem("webUrl",local)
    // 判断用户是否登录:验证 token
     var token = window.sessionStorage.getItem('token')
     
  // var  token = '9df7243689b902de1ba1ededed61c8d1'
  //   window.sessionStorage.setItem('token',token)
    if (!token) {
      router.push('/WechatAuthorization')
    } else {
      next()
    }
  } else {
    next()
  }
})

export default router
